package PAT;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
import java.util.function.Predicate;
import java.util.stream.Collectors;

class Book{
    String id;
    String title;
    String author;
    String keywords;
    String publisher;
    String publishYear;

    public Book(String id, String title, String author, String keywords, String publisher, String publishYear) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.keywords = keywords;
        this.publisher = publisher;
        this.publishYear = publishYear;
    }
}

public class PAT1022 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        ArrayList<Book> books = new ArrayList<>();
        while (n-->0){
            books.add(new Book(scanner.nextLine(),scanner.nextLine(),scanner.nextLine(),scanner.nextLine(),scanner.nextLine(),scanner.nextLine()));
        }
        books.sort(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                return o1.id.compareTo(o2.id);
            }
        });
        int m = scanner.nextInt();
        scanner.nextLine();
        int p = m;
        while (m-->0){
            String s = scanner.nextLine();
            List<Book> deal = deal(s, books);
            if(p!=m+1){
                System.out.print("\n");
            }
            System.out.println(s);
            if(deal.isEmpty()){
                System.out.print("Not Found");
            }else{
                for (int i = 0; i < deal.size(); i++) {
                    System.out.print(deal.get(i).id);
                    if(i<deal.size()-1){
                        System.out.print("\n");
                    }
                }
            }
            System.gc();
        }
    }

    private static List<Book> deal(String s, List<Book> books) {
        String[] split = s.split(": ");
        String type = split[0];
        String value = split[1];
        switch (type){
            case "1":{
                return books.stream().filter(new Predicate<Book>() {
                    @Override
                    public boolean test(Book book) {
                        return book.title.equals(value);
                    }
                }).collect(Collectors.toList());
            }
            case "2":{
                return books.stream().filter(new Predicate<Book>() {
                    @Override
                    public boolean test(Book book) {
                        return book.author.equals(value);
                    }
                }).collect(Collectors.toList());
            }
            case "3":{
                return books.stream().filter(new Predicate<Book>() {
                    @Override
                    public boolean test(Book book) {
                        return book.keywords.contains(value);
                    }
                }).collect(Collectors.toList());
            }
            case "4":{
                return books.stream().filter(new Predicate<Book>() {
                    @Override
                    public boolean test(Book book) {
                        return book.publisher.equals(value);
                    }
                }).collect(Collectors.toList());
            }
            default:{
                return books.stream().filter(new Predicate<Book>() {
                    @Override
                    public boolean test(Book book) {
                        return book.publishYear.equals(value);
                    }
                }).collect(Collectors.toList());
            }
        }
    }
}
